<div class="container-xl">
  <!-- Page title -->
  <div class="page-header d-print-none">
    <div class="row align-items-center">
      <div class="col">
        <h2 class="page-title text-nowrap">
          资源搜索
        </h2>
        {% if Count > 8 %}
        <div class="text-muted mt-1 d-none d-md-block">共搜索到 {{ Count }} 条记录</div>
        {% endif %}
      </div>
      <div class="col-auto ms-auto d-print-none">
        <div class="d-flex btn-list">
          <div class="input-group">
            <input id="search_word" class="form-control" type="text" value="" placeholder="电影/电视剧名称">
            <button id="search_btn" class="btn btn-primary d-none d-sm-inline-block"
              onclick="javascript:navmenu('medialist?s='+$('#search_word').val()+'&f=1&t=search')">搜索</button>
            <button id="search_icon" onclick="javascript:navmenu('medialist?s='+$('#search_word').val()+'&f=1&t=search')" class="btn btn-primary d-sm-none btn-icon">
              <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-search" width="24" height="24"
                viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
                stroke-linejoin="round">
                <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                <circle cx="10" cy="10" r="7"></circle>
                <line x1="21" y1="21" x2="15" y2="15"></line>
              </svg>
            </button>
            <button id="quick_search_btn" data-bs-toggle="dropdown" type="button"
              class="btn btn-primary dropdown-toggle dropdown-toggle-split"
              style="border-top-right-radius:4px;border-bottom-right-radius:4px;"></button>
            <div class="dropdown-menu dropdown-menu-end">
              <a class="dropdown-item" href="javascript:search_media(true)">
                快速搜索
              </a>
              <a class="dropdown-item" href="javascript:show_search_advanced_modal()">
                高级搜索
              </a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<!-- 业务页面代码 -->
{% if Count > 0 %}
<div class="page-body">
  <div class="container-xl">
    <div class="row">
      {% if Count > 8 %}
      <div class="col-md-3 col-lg-2 d-none d-md-block">
        <div class="accordion" id="filter_form_accordion"
          style="position:-webkit-sticky;position:sticky;top:4rem;z-index:1;">
          <div class="accordion-item">
            <h2 class="accordion-header" id="heading-filter_type">
              <button class="accordion-button " type="button" data-bs-toggle="collapse" data-bs-target="#filter_type"
                aria-expanded="true">
                类型
              </button>
            </h2>
            <div id="filter_type" class="accordion-collapse collapse show" data-bs-parent="#filter_form_accordion">
              <div class="accordion-body pt-0">
                <div class="list-group list-group-transparent">
                  {% for mtype in MediaMTypes %}
                  <a class="list-group-item list-group-item-action d-flex align-items-center" href="javascript:void(0)"
                    onclick="filter_medias('filter_type', this)">
                    <span>{{ mtype.name }}</span>
                    <small class="text-muted ms-auto">{{ mtype.num }}</small>
                  </a>
                  {% endfor %}
                </div>
              </div>
            </div>
          </div>
          <div class="accordion-item">
            <h2 class="accordion-header" id="heading-filter_title">
              <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#filter_title"
                aria-expanded="true">
                标题
              </button>
            </h2>
            <div id="filter_title" class="accordion-collapse collapse" data-bs-parent="#filter_form_accordion">
              <div class="accordion-body pt-0">
                <div class="list-group list-group-transparent">
                  {% for name in MediaNames %}
                  <a class="list-group-item list-group-item-action d-flex align-items-center" href="javascript:void(0)"
                    onclick="filter_medias('filter_title', this)">
                    <span>{{ name.name }}</span>
                    <small class="text-muted ms-auto">{{ name.num }}</small>
                  </a>
                  {% endfor %}
                </div>
              </div>
            </div>
          </div>
          <div class="accordion-item">
            <h2 class="accordion-header" id="heading-filter_site">
              <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
                data-bs-target="#filter_site" aria-expanded="true">
                站点
              </button>
            </h2>
            <div id="filter_site" class="accordion-collapse collapse" data-bs-parent="#filter_form_accordion">
              <div class="accordion-body pt-0">
                <div class="list-group list-group-transparent">
                  {% for site in MediaSites %}
                  <a class="list-group-item list-group-item-action d-flex align-items-center" href="javascript:void(0)"
                    onclick="filter_medias('filter_site', this)">
                    <span>{{ site.name }}</span>
                    <small class="text-muted ms-auto">{{ site.num }}</small>
                  </a>
                  {% endfor %}
                </div>
              </div>
            </div>
          </div>
          <div class="accordion-item">
            <h2 class="accordion-header" id="heading-filter_res">
              <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
                data-bs-target="#filter_res" aria-expanded="true">
                质量
              </button>
            </h2>
            <div id="filter_res" class="accordion-collapse collapse" data-bs-parent="#filter_form_accordion">
              <div class="accordion-body pt-0">
                <div class="list-group list-group-transparent">
                  {% for restype in MediaRestypes %}
                  <a class="list-group-item list-group-item-action d-flex align-items-center" href="javascript:void(0)"
                    onclick="filter_medias('filter_res', this)">
                    <span>{{ restype.name }}</span>
                    <small class="text-muted ms-auto">{{ restype.num }}</small>
                  </a>
                  {% endfor %}
                </div>
              </div>
            </div>
          </div>
          <div class="accordion-item">
            <h2 class="accordion-header" id="heading-filter_pix">
              <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
                data-bs-target="#filter_pix" aria-expanded="true">
                分辨率
              </button>
            </h2>
            <div id="filter_pix" class="accordion-collapse collapse" data-bs-parent="#filter_form_accordion">
              <div class="accordion-body pt-0">
                <div class="list-group list-group-transparent">
                  {% for pix in MediaPixs %}
                  <a class="list-group-item list-group-item-action d-flex align-items-center" href="javascript:void(0)"
                    onclick="filter_medias('filter_pix', this)">
                    <span>{{ pix.name }}</span>
                    <small class="text-muted ms-auto">{{ pix.num }}</small>
                  </a>
                  {% endfor %}
                </div>
              </div>
            </div>
          </div>
          <div class="accordion-item">
            <h2 class="accordion-header" id="heading-filter_free">
              <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
                data-bs-target="#filter_spstate" aria-expanded="true">
                促销
              </button>
            </h2>
            <div id="filter_spstate" class="accordion-collapse collapse" data-bs-parent="#filter_form_accordion">
              <div class="accordion-body pt-0">
                <div class="list-group list-group-transparent">
                  {% for spstate in MediaSPStates %}
                  <a class="list-group-item list-group-item-action d-flex align-items-center" href="javascript:void(0)"
                    onclick="filter_medias('filter_spstate', this)">
                    {% if spstate.name == "1.0 1.0" %}
                    <span>普通</span>
                    {% elif spstate.name == "1.0 0.0" %}
                    <span>免费</span>
                    {% elif spstate.name == "2.0 1.0" %}
                    <span>2X</span>
                    {% elif spstate.name == "2.0 0.0" %}
                    <span>2X免费</span>
                    {% elif spstate.name == "1.0 0.5" %}
                    <span>50%</span>
                    {% elif spstate.name == "2.0 0.5" %}
                    <span>2X 50%</span>
                    {% elif spstate.name == "1.0 0.7" %}
                    <span>70%</span>
                    {% elif spstate.name == "1.0 0.3" %}
                    <span>30%</span>
                    {% else %}
                    <span>{{ spstate.name }}</span>
                    {% endif %}
                    <small class="text-muted ms-auto">{{ spstate.num }}</small>
                  </a>
                  {% endfor %}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      {% endif %}
      <div class="{% if Count > 8 %}col-sm-12 col-md-9 col-lg-10{% else %}col-12{% endif %}">
        <div class="row row-cards" id="media_cards">
          {% for Item in Items %}
          <div class="col-12 {% if Count > 8 %}col-xl-4{% else %}col-md-4 col-xl-3{% endif %} media_card" id="media_card_{{ Item[0] }}">
            <div class="card card-sm card-link-pop rounded-3">
              {% if Item[11] > 0 %}
              <div class="badge badge-pill bg-purple" style="position:absolute;top:10px;right:10px;">
                {{ Item[11] }}</div>
              {% endif %}
              {% if Item[9] %}
              <a href='javascript:show_download_modal("{{ Item[0] }}","{{ Item[1] }}")' class="d-block">
                <img src="{{ Item[9] }}" class="card-img-top" title="点击添加下载"
                  style="border-top-left-radius:8px !important; border-top-right-radius:8px !important; object-fit:contain;">
              </a>
              {% endif %}
              <div class="card-body">
                <div class="d-flex align-items-center mb-2">
                  <input type="hidden" name="media_card_type" value="{{ Item[10] }}">
                  <input type="hidden" name="media_card_spstate" value="{{ Item[19] }} {{ Item[20] }}">
                  <input type="hidden" name="media_card_title" value="{{ Item[1] }}">
                  <div>
                    <div>
                      <a
                        href='{% if not Item[9] %}javascript:show_download_modal("{{ Item[0] }}","{{ Item[13] }}"){% else %}javascript:show_download_modal("{{ Item[0] }}","{{ Item[1] }}"){% endif %}'
                        title="点击添加下载">
                      <strong>{% if Item[14] or not Item[13] %}{{ Item[1]|replace("()", "") }}{% else %} {{ Item[13] }} {% endif %}</strong></a>
                      <span class="badge ms-2">{{ Item[4] }}</span>
                        {% if Item[19] != 1.0 %}
                            <span class="badge text-white bg-azure">{{ (Item[19] * 100) | int }}%UL</span>
                        {% endif %}
                        {% if Item[20] != 1.0 %}
                            {% if Item[20] == 0.0 %}
                                <span class="badge text-white bg-lime">FREE</span>
                            {% else %}
                                <span class="badge text-white bg-blue">{{ (Item[20] * 100) | int }}%DL</span>
                            {% endif %}
                        {% endif %}
                    </div>
                    <div class="text-muted mt-1">
                      <strong class="media_card_info"><span class="badge badge-outline text-blue">{{ Item[6]}}</span> <span class="badge badge-outline text-orange">{{ Item[2]}}</span> <span class="badge badge-outline text-yellow">{{ Item[3]}}</span></strong>
                    </div>
                  </div>
                </div>
                {% if Item[12] %}
                <div class="text-muted border-top pt-2">
                  {% if Item[10] == "MOV" %}
                  <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-movie" width="24"
                    height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
                    stroke-linecap="round" stroke-linejoin="round">
                    <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                    <rect x="4" y="4" width="16" height="16" rx="2"></rect>
                    <line x1="8" y1="4" x2="8" y2="20"></line>
                    <line x1="16" y1="4" x2="16" y2="20"></line>
                    <line x1="4" y1="8" x2="8" y2="8"></line>
                    <line x1="4" y1="16" x2="8" y2="16"></line>
                    <line x1="4" y1="12" x2="20" y2="12"></line>
                    <line x1="16" y1="8" x2="20" y2="8"></line>
                    <line x1="16" y1="16" x2="20" y2="16"></line>
                  </svg>
                  {% else %}
                  <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-device-tv" width="24"
                    height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
                    stroke-linecap="round" stroke-linejoin="round">
                    <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                    <rect x="3" y="7" width="18" height="13" rx="2"></rect>
                    <polyline points="16 3 12 7 8 3"></polyline>
                  </svg>
                  {% endif %}
                  <small><a href="{{ Item[17] }}" target="_blank">{{ Item[12] }}</a></small>
                </div>
                {% endif %}
                {% if Item[14] and Item[13] %}
                <div class="text-muted">
                  <small>{{ Item[13] }}</small>
                </div>
                {% endif %}
              </div>
            </div>
          </div>
          {% endfor %}
        </div>
      </div>
    </div>
  </div>
</div>
{% else %}
<div class="page-body">
  <div class="container-xl d-flex flex-column justify-content-center">
    <div class="empty">
      <div class="empty-img"><img src="./static/img/posting_photo.svg" height="128" alt="">
      </div>
      <p class="empty-title" id="search_tip_title">没有记录</p>
      <p class="empty-subtitle text-muted" id="search_tip_text">
        输入想看的电影、电视剧名称，点击搜索试试看吧。
      </p>
    </div>
  </div>
</div>
{% endif %}
<div class="modal modal-blur fade" id="modal-search-advanced" tabindex="-1" role="dialog" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">高级搜索</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <div class="row">
          <div class="col-12 col-lg-4">
            <div class="mb-3">
              <label class="form-label">类型</label>
              <select class="form-select" id="search_type">
                <option value="" selected>全部</option>
                <option value="电影">电影</option>
                <option value="电视剧">电视剧</option>
              </select>
            </div>
          </div>
          <div class="col-12 col-lg-4">
            <div class="mb-3">
              <label class="form-label required">名称</label>
              <input type="text" value="" id="search_name" class="form-control" placeholder="电影/电视剧名称">
            </div>
          </div>
          <div class="col-12 col-lg-2">
            <div class="mb-3">
              <label class="form-label">年份</label>
              <input type="text" value="" id="search_year" class="form-control" placeholder="20xx">
            </div>
          </div>
          <div class="col-12 col-lg-2">
            <div class="mb-3">
              <label class="form-label">季</label>
              <select class="form-select" id="search_season">
                <option value="" selected>全部</option>
              </select>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-12 col-lg-4">
            <div class="mb-3">
              <label class="form-label">站点</label>
              <select class="form-select" id="search_site">
                <option value="" selected>全部</option>
                {% for site in SiteDict %}
                <option value="{{ site }}">{{ site }}</option>
                {% endfor %}
              </select>
            </div>
          </div>
          <div class="col-12 col-lg-4">
            <div class="mb-3">
              <label class="form-label">质量</label>
              <select class="form-select" id="search_restype">
                <option value="" selected>全部</option>
                {% for restype in RestypeDict %}
                <option value="{{ restype }}">{{ restype }}</option>
                {% endfor %}
              </select>
            </div>
          </div>
          <div class="col-12 col-lg-4">
            <div class="mb-3">
              <label class="form-label">分辨率</label>
              <select class="form-select" id="search_pix">
                <option value="" selected>全部</option>
                {% for pix in PixDict %}
                <option value="{{ pix }}">{{ pix }}</option>
                {% endfor %}
              </select>
            </div>
          </div>
          <div class="col-12 col-lg-4">
            <div class="mb-3">
              <label class="form-label">促销</label>
              <select class="form-select" id="sp_state">
                <option value="* *" selected>全部</option>
                <option value="1.0 1.0">普通</option>
                <option value="1.0 0.0">免费</option>
                <option value="2.0 1.0">2X</option>
                <option value="2.0 0.0">2X免费</option>
                <option value="1.0 0.5">50%</option>
                <option value="2.0 0.5">2X 50%</option>
                <option value="1.0 0.7">70%</option>
                <option value="1.0 0.3">30%</option>
              </select>
            </div>
          </div>
          <div class="col-12 col-lg-8">
            <div class="mb-3">
              <label class="form-label">自定义</label>
              <input type="text" value="" id="search_key" class="form-control" placeholder="自定义过滤规则">
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button onclick="javascript:search_media_advanced()" id="search_advanced_btn" class="btn btn-primary">开始搜索</button>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
  // 关键字
  var search_word = "{{ SearchWord }}";
  var need_search = "{{ NeedSearch }}";
  var result_count = {{ Count }}
  if (search_word != "") {
    $("#search_word").val(decodeURI(search_word));
    if (need_search) {
      search_media(true);
    } else if (!result_count) {
      $("#search_tip_title").text(search_word + " 未找到任何资源")
      $("#search_tip_text").text("")
    }
  }
  // 显示下载提示框
  function show_download_modal(id, name) {
    show_ask_modal("是否立即下载 " + name + "？", function () {
      hide_ask_modal();
      download_link(id, name);
    });
  }

  // 搜索按钮
  function search_media(unident) {
    if (!unident) {
      unident = "";
    } else {
      unident = "1";
    }
    var keyword = $("#search_word").val();
    if (keyword == "") {
      return;
    }
    $("#search_btn").text("搜索中...");
    $("#search_btn").attr("disabled", "true");
    $("#quick_search_btn").attr("disabled", "true");
    $("#search_icon").attr("disabled", "true");
    $("#search_tip_title").text("正在搜索 " + keyword + "，请稍后...");
    $("#search_tip_text").text("");
    var param = { "search_word": keyword, "unident": unident };
    show_wait_process();
    ajax_post("search", param, function (ret) {
      hide_wait_process();
      if(ret.code == 0){
        navmenu('search?s=' + keyword);
      }else{
        $("#search_btn").text("搜索");
        $("#search_btn").attr("disabled", false);
        $("#quick_search_btn").attr("disabled", false);
        $("#search_icon").attr("disabled", false);
        show_fail_modal(ret.msg);
      }
    });
  }
  //点击链接下载
  function download_link(id, name) {
    var cmd = "download";
    var data = { "id": id };
    ajax_post(cmd, data, function (ret) {
      if(ret.retcode == 0){
        show_success_modal(name + " 添加下载成功！");
      }else{
        show_fail_modal(name + " 添加下载失败 " + ret.retmsg + "！");
      }
    });
  }

  // 搜索输入框
  $('#search_word').bind('keypress', function (event) {
    if (event.keyCode == "13") {
      navmenu('medialist?s='+$('#search_word').val()+'&f=1&t=search')
    }
  });

  // 执行过滤
  function filter_do() {
    var strings = [];
    var i = 0;
    $("#filter_form_accordion").find("a").each(function () {
      if ($(this).hasClass("active")) {
        strings[i] = $(this).find("span").text().trim();
        i++;
      }
    });

    if (strings.length == 0) {
      $("#media_cards").find("div[class*='media_card']").show();
    } else {
      $("#media_cards").find("div[class*='media_card']").each(function () {
        media_string = $(this).find("strong[class='media_card_info']").text();
        type_string = $(this).find("input[name='media_card_type']").val();
        spstate_string = $(this).find("input[name='media_card_spstate']").val();
        title_string = $(this).find("input[name='media_card_title']").val();
        show_flag = true;
        for (var i = 0; i < strings.length; i++) {
          if (strings[i] == "电影" && type_string != "MOV") {
            show_flag = false;
            break;
          }
          if (strings[i] == "电视剧" && type_string != "TV") {
            show_flag = false;
            break;
          }
          if (strings[i] == "动漫" && type_string != "ANI") {
            show_flag = false;
            break;
          }
          const sp_name_map = new Map([
              ["普通", '1.0 1.0'],
              ["免费", '1.0 0.0'],
              ["2X", '2.0 1.0'],
              ["2X免费", '2.0 0.0'],
              ["50%", '1.0 0.5'],
              ["2X 50%", '2.0 0.5'],
              ["70%", '1.0 0.7'],
              ["30%", '1.0 0.3']
          ]);
          if (Array.from(sp_name_map.keys()).includes(strings[i])) {
              let sp_state = sp_name_map.get(strings[i]);
              if (sp_state != spstate_string) {
                  show_flag = false;
                  break;
              }
          }
          if (strings[i] != "电影" && strings[i] != "电视剧" && strings[i] != "动漫"
              && !Array.from(sp_name_map.keys()).includes(strings[i])
              && media_string.search(strings[i]) == -1
              && title_string.search(strings[i]) == -1) {
            show_flag = false;
            break;
          }
        }
        if (show_flag) {
          $(this).show();
        } else {
          $(this).hide();
        }
      });
    }

  }

  // 过滤媒体展示
  function filter_medias(typeid, obj) {
    // 处理响应
    if (obj) {
      if ($(obj).hasClass("active")) {
        $(obj).removeClass("active");
      } else {
        $(obj).addClass("active");
        if (typeid) {
          $("#" + typeid).find("a").not(obj).removeClass("active");
        }
      }
    }
    // 开始过滤
    filter_do();
  }

  // 重置过滤器
  function reset_filter() {
    $("#filter_form").find("a").removeClass("active");
    $("#filter_size_begin").val("");
    $("#filter_size_end").val("");
    filter_do();
  }

  // 显示高级搜索框
  function show_search_advanced_modal() {
    $("#modal-search-advanced").modal("show");
  }

  // 开始高级搜索
  function search_media_advanced() {
    $("#search_icon").attr("disabled", "true");
    // 读取数据
    var search_name = $("#search_name").val();
    if (!search_name) {
      $("#search_name").addClass("is-invalid");
      return;
    } else {
      $("#search_name").removeClass("is-invalid");
    }
    var search_type = $("#search_type").val();
    var search_season = $("#search_season").val();
    var search_year = $("#search_year").val();
    var search_site = $("#search_site").val();
    var search_restype = $("#search_restype").val();
    var search_pix = $("#search_pix").val();
    var sp_state = $("#sp_state").val();
    var search_key = $("#search_key").val();
    // 拼装请求
    if (search_type) {
      var keyword = search_type + " " + search_name;
    } else {
      var keyword = search_name;
    }
    if (search_year) {
      keyword = keyword + " " + search_year;
    }
    if (search_season) {
      keyword = keyword + " " + search_season;
    }
    var filters = { "site": search_site, "restype": search_restype, "pix": search_pix, "sp_state": sp_state, "key": search_key};
    var param = { "search_word": keyword, "filters": filters, "unident": true};
    $("#search_advanced_btn").text("搜索中...");
    $("#search_advanced_btn").attr("disabled", "true");
    ajax_post("search", param, function (ret) {
      $("#modal-search-advanced").modal("hide");
      navmenu('search?s=' + keyword)
    });
  }

  //初始化下拉框
  $("#search_season").empty();
  $("#search_season").append('<option value="">全部</option>');
  for (var i = 1; i <= 50; i++) {
    $("#search_season").append('<option value="第' + i + '季">第' + i + '季</option>');
  }

</script>